package com.spikything.ui {
	
	import com.spikything.gfx.RoundFrame;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.DropShadowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	//import com.spikything.gfx.Scale9Bitmap;
	import com.spikything.gfx.TextLabel;

	public class LabelledButton extends Sprite {
		
		protected const OVER_TRANS 		:ColorTransform = new ColorTransform(1,1,1,.7,0,0,0,0);
		protected const ALPHA_OVER 		:Number 		= 1;
		protected const ALPHA_UP 		:Number 		= 1;
		protected const ALPHA_DOWN 		:Number 		= .5;
		protected var bgAlpha 			:Number 		= 1;
		//protected const SCALE9PAD 		:Number 		= 8;
		
		//[Embed(source='../../../../assets/gfx/labelled_button_bg.png')]  
		//private var buttonBD 			:Class;
		protected var _mouseOver 		:Boolean;
		protected var _background 		:RoundFrame//:Scale9Bitmap;
		protected var _labelField 		:TextField;
		//protected var round 			:Number 		= Style.ROUND;
		public var id 					:String;
		public var data 				:Object;
		
		public function LabelledButton (label:String = "", x:Number = 0, y:Number = 0, id:String="", data:Object=null) {
			
			this.id = id;
			this.data = data;
			_labelField = new TextLabel(label, Style.BUTTON_TEXT_FORMAT);
			_labelField.x = 6;
			_labelField.y = -2;// SCALE9PAD / 2;
			addChild(_labelField);
			//mouseEnabled = true;
			mouseChildren = false;
			buttonMode = true;
			this.x = x;
			this.y = y;
			
			var buttonWidth 	:Number = _labelField.width + (_labelField.x*2) + 2;
			var buttonHeight 	:Number = 18;// _labelField.height + (_labelField.y * 2);
			//var buttonBitmap:Bitmap = new buttonBD() as Bitmap;
			//_background = new Scale9Bitmap(buttonBitmap.bitmapData, new Rectangle(SCALE9PAD, SCALE9PAD, buttonBitmap.bitmapData.width-(SCALE9PAD*2), buttonBitmap.bitmapData.height-(SCALE9PAD*2)));
			_background = new RoundFrame(buttonWidth, buttonHeight, Style.BRAND_COLOUR, bgAlpha, getRounding());
			//_background.width = buttonWidth;
			//_background.height = buttonHeight;
			addChildAt(_background, 0);
			
			//_labelField.filters = [new DropShadowFilter(1,90,0,.4,2,2,1,1)];
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
			addEventListener(MouseEvent.ROLL_OUT, onMouseOut);
		}
		
		public function clicked (e:Event):void {
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		public function get mouseOver ():Boolean { return _mouseOver };
		public function set mouseOver (value:Boolean):void {
			_mouseOver = value;
			transform.colorTransform = _mouseOver ? OVER_TRANS : new ColorTransform();
			_background.alpha = _mouseOver ? ALPHA_OVER : ALPHA_UP;
		}
		
		public function onMouseDown (e:MouseEvent):void { _background.alpha = ALPHA_DOWN };
		public function onMouseUp (e:MouseEvent):void { mouseOver = mouseOver };
		public function onMouseOver (e:MouseEvent):void { mouseOver = true };
		public function onMouseOut (e:MouseEvent):void { mouseOver = false };
		
		public function get background ():RoundFrame { return _background }; //Scale9Bitmap
		
		public function get _width ():Number { return _background.width };
		public function set _width (value:Number):void { _background.width = value };
		
		public function get _height ():Number { return _background.height };
		public function set _height (value:Number):void { _background.height = value };
		
		public function get label ():TextField { return _labelField };
		public function get text ():String { return label.text };
		public function set text (value:String):void { label.text = value };
		
		protected function getRounding ():Number {
			return Style.ROUND;
		}
		
	}
	
}